/*

Copyright 2009 Nicola Dal Pont

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

package com.astoolkit.workflow.task.log
{
	import com.astoolkit.workflow.BasePipelineTask;
	import com.astoolkit.workflow.BaseTask;
	import com.astoolkit.workflow.IPipelineTask;
	
	import flash.utils.getQualifiedClassName;
	import flash.utils.setTimeout;
	
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.logging.LogEventLevel;

	public class WriteLog extends BasePipelineTask
	{

		
		private static const LOGGER : ILogger = 
			Log.getLogger( getQualifiedClassName( WriteLog ).replace(/:+/g, "." ) );
		
		public var message : String = null;
		public var parameters : Array = [];
		
		[Inspectable(defaultValue="debug", enumeration="debug,info,warn,error,fatal")]
		public var level : String = "debug";
		
		private var _levels : Object =
			{
				debug : LogEventLevel.DEBUG,
				info : LogEventLevel.INFO,
				warn : LogEventLevel.WARN,
				error : LogEventLevel.ERROR,
				fatal : LogEventLevel.FATAL
			};
		
		override public function begin( inRetain : Boolean = false ) : void
		{
			super.begin( inRetain );
			try 
			{
				if( !message )
				{
					if( _pipelineData ) 
						message = _pipelineData.toString();
					else
						message = "[No message provided]";
				}
				var outMessage : String = message;
				var varName : String;
				var re : RegExp;
				while( outMessage.match( /\$\w+/ ) )
				{
					varName = outMessage.match( /\$\w+/ )[0];
					re = new RegExp( "\\\u0024" + varName.substr(1) + "", "g" );
					outMessage = outMessage.replace( re, context.variables[ varName.substr(1) ] );
				}
				var args : Array = [ _levels[ level ], outMessage ];
				args = args.concat( parameters );
				
				if( LOGGER )
				{
					var logFn : Function  = LOGGER[ "log" ] as Function;
					if( logFn != null )
						logFn.apply( LOGGER, args );
				}
				complete();
			}
			catch( e : Error )
			{
				fail( e.message );
			}
		}
		

	}
}